package com.mstore.util;

import java.io.UnsupportedEncodingException;

import com.mstore.config.Constants;
import com.mstore.pojo.BWeixinurl;
import com.mstore.pojo.ThirdAct;

public class WeixinUtils {
	
	public static String getWeixinUrl(ThirdAct thirdAct, BWeixinurl bWeixinurl) {
		// 模板类
		String templateUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=123#wechat_redirect";

		// 微信网关处理回调的URL地址，从数据库读取
		String weixinCallbackUrl = Constants.weixinCallbackUrl;
		
		// 企业微信号的原始ID，从数据库读取
		String ysId = thirdAct.getYsId();
		// wumiMember系统处理用户的登录的url,从数据库读取
		String wumiMemberUrl = "&url="+bWeixinurl.getId();

		String redirectUrl = urlEncodeUTF8(weixinCallbackUrl + ysId + wumiMemberUrl);
		
		String lastUrl = templateUrl.replace("APPID", thirdAct.getAppId())
				.replace("REDIRECT_URI", redirectUrl);
		
		return lastUrl;
	}

	/**
	 * URL编码（utf-8）
	 * 
	 * @param source
	 * @return
	 */
	public static String urlEncodeUTF8(String source) {
		String result = source;
		try {
			result = java.net.URLEncoder.encode(source, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return result;
	}
	
	public static void main(String[] args) {
		
	}
	
}
